package com.bluefuture.modules.job.task;

import com.bluefuture.common.utils.DateUtils;
import com.bluefuture.modules.app.entity.AppOrderEntity;
import com.bluefuture.modules.app.service.AppOrderService;
import com.bluefuture.modules.app.service.UserService;
import com.bluefuture.modules.app.service.impl.IosServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: leo
 * @date: 2022/2/8
 */
@Component("huberTak")
public class HuberTask {
    @Autowired
    private AppOrderService appOrderService;
    @Autowired
    private UserService userService;
    @Autowired
    private IosServiceImpl iosService;

//    @Scheduled(cron = "0 0/30 * * * ?")
//    public void synIOSPurchase(){
//        System.out.println("【同步苹果内购订单开始】");
//        List<AppOrderEntity> appOrderEntities = appOrderService.waitForCheckList();
//        for (AppOrderEntity appOrderEntity : appOrderEntities) {
//            iosService.iosInAppPurchase(appOrderEntity);
//        }
////        System.out.println("每30分钟同步一次");
//
//    }
    @Scheduled(cron = "0 30 1 * * ?")
    public void synUserMembershipType(){
        System.out.println("【同步会员类型开始】");
        //即将过期的
        List<AppOrderEntity> appOrderEntities = appOrderService.aboutToExpireList();
        for (AppOrderEntity appOrderEntity : appOrderEntities) {
            Integer type = appOrderEntity.getType();
//            1-月度，2-季度，3-年度
            switch (type){
                case 1:
                    if (DateUtils.getBetweenDays(new Date(),appOrderEntity.getPurchaseDate())>31){
                        if (appOrderEntity.getStatus()==2){
                            userService.updateUserToNormalUser(appOrderEntity.getUserId().longValue());
                        }
                        appOrderEntity.setStatus(3);
                        appOrderEntity.setUpdateTime(new Date());
                        appOrderService.updateById(appOrderEntity);
                    }
                    break;
                case 2:
                    if (DateUtils.getBetweenDays(new Date(),appOrderEntity.getPurchaseDate())>31*3){
                        if (appOrderEntity.getStatus()==2){
                            userService.updateUserToNormalUser(appOrderEntity.getUserId().longValue());
                        }
                        appOrderEntity.setStatus(3);
                        appOrderEntity.setUpdateTime(new Date());
                        appOrderService.updateById(appOrderEntity);
                    }
                    break;
                case 3:
                    if (DateUtils.getBetweenDays(new Date(),appOrderEntity.getPurchaseDate())>31*12){
                        if (appOrderEntity.getStatus()==2){
                            userService.updateUserToNormalUser(appOrderEntity.getUserId().longValue());
                        }
                        appOrderEntity.setStatus(3);
                        appOrderEntity.setUpdateTime(new Date());
                        appOrderService.updateById(appOrderEntity);
                    }
                    break;
                default:
                    break;
            }
        }
//        System.out.println("每天凌晨30分钟同步一次");

    }

    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        instance.set(Calendar.MONTH,0);
        System.out.println(instance.getTime());
        System.out.println(DateUtils.getBetweenDays(new Date(), instance.getTime()));
    }
}
